package luanluan.control.dlg
{
	import com.greensock.TweenLite;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import luanluan.control.buttons.LButton;
	import luanluan.control.managers.StageManager;
	import luanluan.utils.LuanUtils;
	
	/**
	 * 对话框基类
	 * @author luanluan
	 */
	public class BasicDlg
	{
		private static var _container:Sprite;
		
		public static function set dlgLayer(container:Sprite):void
		{
			if (_container)
				throw new Error("已设置层!")
			_container = container;
		}
		
		private var _view:DisplayObject;
		private var _closeBtn:LButton;
		
		public function BasicDlg(view:DisplayObject)
		{
			_view = view;
			_view.x = StageManager.instance.stage.stageWidth / 2;
			_view.y = StageManager.instance.stage.stageHeight / 2;
			if (_view.hasOwnProperty("closeBtn"))
			{
				_closeBtn = new LButton(_view["closeBtn"]);
				_closeBtn.onMouseClick = closeHandler;
			}
		}
		
		private function closeHandler(e:MouseEvent):void
		{
			hide();
		}
		
		/**
		 * 显示对话框
		 * @param	... arg
		 */
		public function show(... arg):void
		{
			_container.addChild(_view);
			_view.alpha = 0;
			TweenLite.to(_view, 0.3, { alpha: 1, onComplete: showComplete } );
		}
		
		/**
		 * 隐藏对话框
		 * @param	... arg
		 */
		public function hide(... arg):void
		{
			_view.alpha = 1;
			TweenLite.to(_view, 0.3, { alpha: 0, onComplete: hideComplete } );
			if (_closeBtn)
				_closeBtn.enable = false;
		}
		
		protected function showComplete():void
		{
			if(_closeBtn)
				_closeBtn.enable = true;
		}
		
		protected function hideComplete():void
		{
			LuanUtils.removeDisplayObj(_view);
		}
	}

}